package ginsan.feignDemo.controller;

import ginsan.feignDemo.remote.GitHubClient;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.Optional;

@RestController
public class MyController {
    final Logger LOGGER = LoggerFactory.getLogger(MyController.class);
    private GitHubClient gitHubClient;

    public MyController(final GitHubClient gitHubClient) {
        this.gitHubClient = gitHubClient;
    }

    @GetMapping("/greeting")
    public ResponseEntity<String> sayHi() {
        LOGGER.info("hello there !");
        return Optional.of(Math.random())
                .filter(it -> it > 0.5)
                .map(it ->
                        ResponseEntity.ok(" Ginsan says hello to you ! ")
                ).orElse(ResponseEntity.notFound().build());
    }

    @GetMapping("/login")
    public ResponseEntity login() {
        return ResponseEntity.ok(gitHubClient.login());
    }

}